package com.cpfmat.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;
import com.cpfmat.common.CommonStringUtils;
import com.cpfmat.common.Contains;
import com.cpfmat.common.LogUtils;
import com.cpfmat.entity.Attachment;
import com.cpfmat.entity.ContentUser;
import com.cpfmat.entity.SystemUser;
import com.cpfmat.service.IAttachmentService;
import com.cpfmat.service.IContentUserService;
import com.cpfmat.service.IRzTabService;

@Controller
public class ContentUserManageController {
	@Autowired
	private IContentUserService contentUserService;
	@Autowired
	private IAttachmentService attachmentService;
	@Autowired
	private IRzTabService rzTabService;

	@RequestMapping(path = "/manage/searchContent")
	public ModelAndView searchUser(@RequestParam(defaultValue = "1", name = "pageNum") int pageNum,
			@RequestParam(required = false, name = "userName") String userName,
			@RequestParam(required = false, name = "subTitle") String subTitle, HttpServletRequest request) {

		ModelAndView view = new ModelAndView("/manage/otherUser/contentUserAjaxtr");
		Page<ContentUser> page = contentUserService.getAllPage(userName, subTitle, pageNum, Contains.PAGE_SIZE);
		view.addObject("page", page);
		return view;
	}

	@RequestMapping(path = "/manage/contentList")
	public ModelAndView init() {
		ModelAndView view = new ModelAndView("/manage/otherUser/contentUserList");
		return view;
	}

	@RequestMapping(path = "/manage/contenttg")
	public ModelAndView contenttg(@RequestParam(required = false, name = "fileId") String fileId,
			@RequestParam(required = false, name = "tgbz") String tgbz,
			@RequestParam(required = false, name = "finalRejection") String finalRejection,
			HttpServletRequest request) {
		ModelAndView view = new ModelAndView("/manage/otherUser/contentUserAjaxtr");

		ContentUser content = contentUserService.selectById(fileId);
		// 获取当前后台登录管理员
		SystemUser user = (SystemUser) request.getSession().getAttribute("loginUser");
		if (content != null) {
			content.setShzt(tgbz);
			if ("Y".equalsIgnoreCase(tgbz)) {
				rzTabService.log(null, "报送材料通过审核", CommonStringUtils.getIpAddress(request), user.getSeqId());
			} else if ("N".equalsIgnoreCase(tgbz)) {
				if (StringUtils.isNotBlank(finalRejection)) {
					finalRejection = finalRejection.trim();
					if (finalRejection.length() <= 255) {
						content.setFinalRejection(finalRejection);
						rzTabService.log(null, "报送材料不通过审核", CommonStringUtils.getIpAddress(request), user.getSeqId());
					} else {
						view.addObject("flag", "FR_TL");
					}
				} else {
					view.addObject("flag", "FR_NP");
				}
			} else {
				view.addObject("flag", "FG_NP");
			}
			try {
				contentUserService.customUpdate(content);
			} catch (Exception e) {
				view.addObject("flag", "DB_ER");
				LogUtils.debug(e);
			}
		} else {
			view.addObject("flag", "DB_ER");
		}
		Page<ContentUser> page = contentUserService.getAllPage(null, null, 1, Contains.PAGE_SIZE);
		view.addObject("page", page);

		return view;
	}

	@RequestMapping(path = "/manage/contentUserDetail")
	public ModelAndView contenttg(@RequestParam(required = false, name = "seqId") String seqId) {
		ModelAndView view = new ModelAndView("/manage/otherUser/contentUserPreview");

		ContentUser contentUser = contentUserService.getObjectById(seqId);
		view.addObject("contentUser", contentUser);

		if (contentUser != null && contentUser.getColumnId() != null) {
			List<Attachment> attachments = attachmentService
					.selectBatchIds(Arrays.asList(contentUser.getColumnId().split("#")));
			view.addObject("attachments", attachments);
		}

		return view;
	}

	@RequestMapping(path = "/manage/editContentUser")
	public ModelAndView editContentUser(@RequestParam(required = false, name = "seqId") String seqId) {
		ModelAndView view = new ModelAndView("/manage/otherUser/contentUserDetail");

		ContentUser contentUser = null;
		if (StringUtils.isNotBlank(seqId)) {
			contentUser = contentUserService.getObjectById(seqId);
			view.addObject("contentUser", contentUser);
		}

		if (contentUser != null && contentUser.getColumnId() != null) {
			List<Attachment> attachments = attachmentService
					.selectBatchIds(Arrays.asList(contentUser.getColumnId().split("#")));
			view.addObject("attachments", attachments);
		}

		return view;
	}

	@RequestMapping(path = "/manage/saveContentUser")
	@ResponseBody
	public String saveContentUser(HttpServletRequest request, String seqId, String title, String subTitle,
			String content, String attachementIds, String specialField, String submission, String tecExp, String unit,
			String contactPerson, String contactPhone) {
		SystemUser user = (SystemUser) request.getSession().getAttribute("loginUser");
		
		if (user == null) {
			return "NO_LG";
		}

		if (StringUtils.isBlank(title)) {
			return "TT_NP";
		} else if (title.length() > 200) {
			return "TT_TL";
		}

		/*if (StringUtils.isBlank(subTitle)) {
			return "ST_NP";
		} else if (subTitle.length() > 200) {
			return "ST_TL";
		}*/
		
		if (StringUtils.isBlank(submission)) {
			return "SB_NP";
		} else if (submission.length() > 200) {
			return "SB_TL";
		}
		
		if (StringUtils.isBlank(tecExp)) {
			return "TE_NP";
		} else if (tecExp.length() > 200) {
			return "TE_TL";
		}
		
		if (StringUtils.isBlank(unit)) {
			return "UN_NP";
		} else if (unit.length() > 200) {
			return "UN_TL";
		}
		
		if (StringUtils.isBlank(contactPerson)) {
			return "CP_NP";
		} else if (contactPerson.length() > 200) {
			return "CP_TL";
		}
		
		contactPhone = contactPhone != null ? contactPhone.trim() : contactPhone;
		if (StringUtils.isBlank(contactPhone)) {
			return "PP_NP";
		} else if (contactPhone.length() > 200) {
			return "PP_TL";
		} else if (!contactPhone.matches("[\\d\\-]{5,}")) {
			return "PP_CN";
		}

		if (StringUtils.isBlank(content)) {
			return "CN_NP";
		}

		if (specialField == null || Contains.specialFieldMap.get(specialField) == null) {
			return "SP_NP";
		}

		try{
		if (StringUtils.isBlank(seqId)) {

			ContentUser contentUser = new ContentUser();
			contentUser.setSqlId(CommonStringUtils.getUUID());
			contentUser.setColumnId(attachementIds);

			contentUser.setContent(content);
			contentUser.setTitle(title);
			contentUser.setSubTitle(subTitle);
			contentUser.setShzt("Y");
			contentUser.setUserId(null);
			contentUser.setCzsj(new Date());
			contentUser.setSpecialField(specialField);
			
			contentUser.setSubmission(submission);
			contentUser.setTecExp(tecExp);
			contentUser.setUnit(unit);
			contentUser.setContactPerson(contactPerson);
			contentUser.setContactPhone(contactPhone);
			
			contentUser.setFinalRejection("@#" + user.getSeqId());

			contentUser.insert();
			// 记录日志
			rzTabService.log(null, "新增报送材料", CommonStringUtils.getIpAddress(request), user.getSeqId());
		} else {
			ContentUser contentUser = new ContentUser();
			contentUser.setSqlId(seqId);
			
			contentUser.setColumnId(attachementIds);

			contentUser.setContent(content);
			contentUser.setTitle(title);
			contentUser.setSubTitle(subTitle);
			contentUser.setShzt("Y");
			contentUser.setUserId(null);
			contentUser.setCzsj(new Date());
			contentUser.setSpecialField(specialField);
			
			contentUser.setSubmission(submission);
			contentUser.setTecExp(tecExp);
			contentUser.setUnit(unit);
			contentUser.setContactPerson(contactPerson);
			contentUser.setContactPhone(contactPhone);
			contentUser.setFinalRejection("@#" + user.getSeqId());
			
			contentUserService.customUpdate(contentUser);
			
			// 记录日志
			rzTabService.log(null, "修改报送材料", CommonStringUtils.getIpAddress(request), user.getSeqId());
		}
		} catch(Exception e){
			e.printStackTrace();
			return "DB_ER";
		}

		return "success";
	}
	
	@RequestMapping("/manage/contentUser/del")
	@ResponseBody
	public boolean del(String seqId, HttpServletRequest request) {
		SystemUser user = (SystemUser) request.getSession().getAttribute("loginUser");

		ContentUser content = contentUserService.selectById(seqId);
		if (user != null && user.getSeqId() != null) {
			if (content != null && ("@#" + user.getSeqId()).equals(content.getFinalRejection())) {
				try{
					rzTabService.log(null, "删除报送材料", CommonStringUtils.getIpAddress(request), user.getSeqId());
	
					content.deleteById();
	
					return true;
				} catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return false;

	}
}
